package implementation.sort.MergeSort;


import anaydis.sort.SorterType;

import java.util.Comparator;
import java.util.List;

public class MergeBottomUp extends AbstractMergeSort {

    public MergeBottomUp() {
        super(SorterType.MERGE_BOTTOM_UP);
    }

    public <T> void sort(Comparator<T> tComparator, List<T> ts) {
        sort(tComparator, ts, 0, ts.size() - 1);
    }

    private<T> void sort(Comparator tComparator, List<T> ts, int start, int end ){
        if (end <= start) return;
        for (int m = 1; m <= end - start; m = m + m) {
            for (int i = start; i <= end - m; i += m + m) {
                merge(tComparator, ts, i, i + m - 1, Math.min(i + m + m - 1, end));
            }
        }
    }

}
